package com.glodon.utils.service;

import java.io.IOException;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;

public class MessageSendService {

	private static final Logger log = LoggerFactory.getLogger(MessageSendService.class);

	private ProducerBean producer ;

	private String topic ;


	public MessageSendService(ProducerBean producer , String topic){
		this.producer = producer ;
		this.topic = topic ;
	}

	private SendResult sendMessage(String tag , byte[] msg){
		Message message = new Message(topic, tag, msg);
		SendResult sendResult = producer.send(message);
		if(sendResult != null){
			log.info("{} 发送消息成功!  Topic : {} ，tag {} ， msgId: {}" , new Date() ,topic , tag , sendResult.getMessageId());
		}
		return sendResult;
	}

	public SendResult sendMessage(String tag , String msg) {
		return sendMessage(tag , msg.getBytes());
	}
}
